#include "StdAfx.h"
#include "Subline.h"

#include "../InterfaceCircuit/ICircuit.h"
#include "../InterfaceCircuit/CircuitUnit.h"
CSubline::CSubline(void)
{
	//m_lStartPinId = -1;
	//m_lEndCircuitUnitId = -1;
	m_nPtIndex = 0;
	m_nPtCount =0;
	//m_pStartPin = NULL;
	//m_pEndCircuitUnit = NULL;

	m_pStartEnt = NULL;
	m_pEndEnt = NULL;
	m_lStartEntId = -1;
	m_lEndEntId = -1;
}

CSubline::CSubline( const CSubline* pSubline )
{
	//m_lStartPinId = pSubline->GetStartPinId();
	//m_lEndCircuitUnitId = pSubline->GetEndCircuitUnitId();
	m_nPtIndex = pSubline->GetPtIndex();
	m_nPtCount = pSubline->GetPtCount();

	CSubline *pTempSubline = const_cast<CSubline *>(pSubline);;
	m_pStartEnt = pTempSubline->GetStartEnt();
	m_pEndEnt = pTempSubline->GetEndEnt();
	m_lStartEntId = pTempSubline->GetStartEntId();
	m_lEndEntId = pTempSubline->GetEndEntId();
}

CSubline::CSubline( CEntityObj *pParentEnt ) : CEntityObj(pParentEnt)
{
	//m_lStartPinId = -1;
	//m_lEndCircuitUnitId = -1;
	m_nPtIndex = 0;
	m_nPtCount =0;
	//m_pStartPin = NULL;
	//m_pEndCircuitUnit = NULL;
	if (pParentEnt->IsSubSystem())
	{
		((CSubSystem*)pParentEnt)->AddSubline(this);
	}

	m_pStartEnt = NULL;
	m_pEndEnt = NULL;
	m_lStartEntId = -1;
	m_lEndEntId = -1;
}


CSubline::~CSubline(void)
{
	CEntityObj::Clear();
}

bool CSubline::IsSubline() const
{
	return true;
}

void CSubline::Serialize( CArchive& ar )
{
	if (ar.IsStoring())
	{
		ar<<m_lStartEntId;
		ar<<m_lEndEntId;
		ar<<m_nPtIndex;
// 		ar<<m_lStartEntId;
// 		ar<<m_lEndEntId;

	}
	else
	{
		ar>>m_lStartEntId;
		ar>>m_lEndEntId;
		ar>>m_nPtIndex;
// 		if( CObj::m_lFileVersion > 55 )
// 		{
// 			ar>>m_lStartEntId;
// 			ar>>m_lEndEntId;
// 		}
	}
	CEntityObj::Serialize(ar);
}

CObj* CSubline::Clone() const
{
	return new CSubline(this);
}

void CSubline::Transform( const CMatrix transf )
{

}

bool CSubline::GetBox( double & dblLeft, double & dblBottom, double & dblRight, double & dblTop ) const
{
	return true;
}

void CSubline::CopyDisplayObj( long lSourceLayerId, long lDestLayerId )
{
	CEntityObj::CopyDisplayObj(lSourceLayerId,lDestLayerId);
}

 

// long CSubline::GetStartPinId() const
// {
// 	return m_lStartPinId;
// }
// 
// long CSubline::GetEndCircuitUnitId() const
// {
// 	return m_lEndCircuitUnitId;
// }
int CSubline::GetPtIndex() const
{
	return m_nPtIndex;
}

// void CSubline::SetStartPinId( long lPinId )
// {
// 	m_lStartPinId  = lPinId;
// }
// 
// 
// void CSubline::SetEndCircuitUnitId( long lUnitId )
// {
// 	m_lEndCircuitUnitId = lUnitId;
// }
void CSubline::SetPtIndex( int nIndex )
{
	m_nPtIndex = nIndex;
}

void CSubline::DeleteDisplayObj( long lLayerId )
{

	CEntityObj::DeleteDisplayObj(lLayerId);
}

CPin* CSubline::GetStartPin()
{
	if (!m_pStartEnt)	
	{
		std::list<CConnector*> lstConn;
		((CSubSystem*)m_pParentEntity)->GetAllConnectorsEx(lstConn);
		std::list<CConnector*>::iterator it_Conn = lstConn.begin();
		for (;it_Conn!=lstConn.end();it_Conn++)
		{
			CConnector* pConn = *it_Conn;
			std::list<CPin*>::const_iterator it_Pin =  pConn->GetPinList().begin();
			for (;it_Pin!=pConn->GetPinList().end();it_Pin++)
			{
				CPin* pPin = *it_Pin;
				if (m_lStartEntId== pPin->GetId())
				{
					m_pStartEnt = pPin;
					return (CPin*)m_pStartEnt;
				}
			}
		}
	}

	if (m_pStartEnt && m_pStartEnt->IsPin() )
	{
		return (CPin*)m_pStartEnt;
	}
	else
	{
		return NULL;
	}
}

// CCircuitUnit* CSubline::GetEndCicuitUnit()
// {
// 	if (!m_pEndCircuitUnit)
// 	{
// 		//std::list<CDevice*> lstDevice;
// 		//((CSubSystem*)m_pParentEntity)->GetAllDevices(lstDevice);
// 		//std::list<CDevice*>::iterator it_Dev = lstDevice.begin();
// 		//for (;it_Dev!=lstDevice.end();it_Dev++)
// 		//{
// 		//	CDevice* pDev = *it_Dev;
// 		//	std::list<CEntityObj*> lstICircuit;
// 		//	pDev->GetICircuitContentList(lstICircuit);
// 		//	for (std::list<CEntityObj*>::iterator it_IC = lstICircuit.begin();
// 		//		it_IC!=lstICircuit.end();it_IC++)
// 		//	{
// 		//		CICircuit* pICircuit = (CICircuit*)(*it_IC);
// 		//		std::list<CCircuitUnit*> lstUnits = pICircuit->GetCircuitUnitList();
// 		//		for (std::list<CCircuitUnit*>::iterator it_unit = lstUnits.begin();
// 		//			it_unit!=lstUnits.end();it_unit++)
// 		//		{
// 		//			CCircuitUnit* pCircuitUnit = *it_unit;
// 		//			if (pCircuitUnit->GetId() == m_lEndCircuitUnitId)
// 		//			{
// 		//				m_pEndCircuitUnit = pCircuitUnit;
// 		//				return m_pEndCircuitUnit;
// 		//			}
// 		//		}
// 
// 		//	}
// 		//}
// 
// 
// 	}
// 	ASSERT(m_pEndCircuitUnit);
// 	return m_pEndCircuitUnit;
// }

// void CSubline::SetStartPin(CPin* pPin)
// {
// 	m_pStartPin = pPin;
// }
// 
// void CSubline::SetEndCicuitUnit(CCircuitUnit* pUnit)
// {
// 	m_pEndCircuitUnit = pUnit;
// }

int CSubline::GetPtCount() const
{
	return m_nPtCount;
}

void CSubline::SetPtCount( int nCount )
{
	m_nPtCount = nCount;
}




void CSubline::SetStartEntId( long id )
{
	m_lStartEntId = id;
}

void CSubline::SetEndEntId( long id )
{
	m_lEndEntId = id;
}

void CSubline::SetStartEnt( CEntityObj* pStartEnt )
{
	m_pStartEnt = pStartEnt;

// 	if( pStartEnt->IsPin() )
// 	{
// 		m_pStartPin = (CPin*)pStartEnt;
// 	}
}

void CSubline::SetEndEnt( CEntityObj* pEndEnt )
{
	m_pEndEnt = pEndEnt;

// 	if( pEndEnt->GetParentEntity()->IsICircuit() )
// 	{
// 		m_pEndCircuitUnit = (CCircuitUnit*)pEndEnt;
// 	}
}
 
long CSubline::GetStartEntId() const
{
	return m_lStartEntId;
}

long CSubline::GetEndEntId() const
{
	return m_lEndEntId;
}

CEntityObj* CSubline::GetStartEnt()
{
	return m_pStartEnt;
}

CEntityObj* CSubline::GetEndEnt()
{
	return m_pEndEnt;
}
extern bool DeleteDisplayObjAndKey(CDisplayObj *pDisplayObj);
void CSubline::DeleteAllDisObj()
{
	list<CDisplayObj*> lstLineDis=this->GetDisplayObjList();
	list<CDisplayObj*>::iterator iter_dis= lstLineDis.begin();
	for (;iter_dis!=lstLineDis.end();iter_dis++)
	{
		CSublineDisplayObj* pLineDis=(CSublineDisplayObj*)*iter_dis;
		pLineDis->DeleteTrackerDisObj();
		pLineDis->DeleteDisplayObjAndKey();
	}
}

CDisplayObj * CSubline::CreateDisplayObj( long lLayerId )
{
	CSublineDisplayObj* pSublineDisObj = new CSublineDisplayObj(this);
	pSublineDisObj->SetLayerId(lLayerId);

	return pSublineDisObj;
}
